package com.giCompiler.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.giCompiler.bean.CodigoFonte;
import com.giCompiler.bean.Pacote;
import com.giCompiler.util.HibernateUtil;

public class PacoteDao {

	public void inserir(Pacote pacote){ 
	       
		Session session = HibernateUtil.getSessionFactory().openSession(); 		       
		Transaction transaction = null; 		       
		       
		try { 
		            
			transaction = session.beginTransaction(); 		           
		    session.save(pacote); 		           
			transaction.commit(); 
		        
		} catch (HibernateException e) { 
		           
			transaction.rollback(); 			           
			e.printStackTrace(); 
		      
		} finally { 		            
			session.close(); 	        
		} 
		
	}	
	
	public void excluir(Pacote pacote){
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try{
			
			transaction = session.beginTransaction();
			session.delete(pacote);
			transaction.commit();
			
		}catch (HibernateException e) {
			
			transaction.rollback(); 			           
			e.printStackTrace();
			
		}finally{			
			session.clear();			
		}		
		
	}
	
	public void excluirPacotesDoProjeto(int idProjeto){
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		        
		try { 
		            
			transaction = session.beginTransaction(); 			           
							 			 
			Query query = session.createSQLQuery("DELETE FROM gicompiler.TB_PACOTE WHERE ID_PROJETO = :idProjeto");					 			 
			
			query.setInteger("idProjeto", idProjeto);
			query.executeUpdate();

			transaction.commit(); 
		       
		}catch (HibernateException e) { 
		        
			transaction.rollback(); 			           
			e.printStackTrace(); 
		       
		} finally { 		           
			session.close(); 		       
		} 
			
	}	

	@SuppressWarnings("unchecked")
	public List<Pacote> pegarPacoteRaiz(int idProjeto){
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		List<Pacote> pacotes = new ArrayList <Pacote> ();
		        
		try { 

		    transaction = session.beginTransaction(); 
		    pacotes = session.createCriteria(Pacote.class).add(Restrictions.eq("idProjeto", idProjeto)).add(Restrictions.eq("idPacSup", 0)).list();	    
			transaction.commit(); 
		
		
		}catch (HibernateException e) { 
		        
			transaction.rollback(); 			           
			e.printStackTrace(); 
		       
		} finally { 		           
			session.close(); 		       
		} 
		
		return pacotes;		
	}
	
	@SuppressWarnings("unchecked")
	public List<Pacote> pegarPacote(int idPacSup, int idProjeto, Session session){
      
		Transaction transaction = null; 
		List<Pacote> pacotes = new ArrayList <Pacote> ();
		        
		try { 

		    transaction = session.beginTransaction(); 
		    pacotes = session.createCriteria(Pacote.class).add(Restrictions.eq("idPacSup", idPacSup)).add(Restrictions.eq("idProjeto", idProjeto)).list();	    
			transaction.commit(); 
		
		
		}catch (HibernateException e) { 
		        
			transaction.rollback(); 			           
			e.printStackTrace(); 
		       
		}
		
		return pacotes;		
	}
		
	@SuppressWarnings("unchecked")
	public List<CodigoFonte> pegarClassesPacoteDefault(int idProjeto){
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null; 
		List<CodigoFonte> cfs = new ArrayList<CodigoFonte>();
		
		try{
			
		    transaction = session.beginTransaction(); 
		    	    
			Query query = session.createSQLQuery("SELECT CF.ID_CODIGO_FONTE, CF.CODIGO_FONTE, CF.DATA_CRIACAO, CF.DATA_MODIFICACAO, CF.ID_PACOTE, CF.ID_USUARIO, CF.NOME FROM gicompiler.TB_PACOTE P, gicompiler.TB_CODIGO_FONTE CF WHERE P.ID_PROJETO = :idProjeto AND P.NOME = \"default\" AND P.ID_PACOTE = CF.ID_PACOTE").addEntity(CodigoFonte.class);						 			 
			query.setInteger("idProjeto", idProjeto);
			cfs = query.list();
		    
		    transaction.commit(); 
					
		} catch (HibernateException e) { 
	        
			transaction.rollback(); 		            
			e.printStackTrace(); 
		        
		}
		
		return cfs;
		
	}

	@SuppressWarnings("unchecked")
	public List<Pacote> pegarPacoteDefault(int idProjeto){
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null; 
		List<Pacote> pacotes = new ArrayList<Pacote>();
		
		try{

		    transaction = session.beginTransaction(); 
		    pacotes = session.createCriteria(Pacote.class).add(Restrictions.eq("idProjeto", idProjeto)).add(Restrictions.eq("nome", "default")).add(Restrictions.eq("idPacSup", 0)).list();	    
			transaction.commit(); 
			
		} catch (HibernateException e) { 
	        
			transaction.rollback(); 		            
			e.printStackTrace(); 
		        
		}
		
		return pacotes;
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Pacote> verificarDisponibilidadeNomePacote(int idPacSup, int idProjeto, String nomePacote){
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<Pacote> pacotes = new ArrayList<Pacote>();
		
		try{
		
			transaction = session.beginTransaction();
			
			Query query = session.createSQLQuery("SELECT * FROM gicompiler.TB_PACOTE WHERE ID_PAC_SUP = :idPacSup AND ID_PROJETO = :idProjeto AND NOME LIKE :nomePacote");
			query.setString("nomePacote", nomePacote);
			query.setInteger("idPacSup", idPacSup);
			query.setInteger("idProjeto", idProjeto);
			pacotes = query.list();
			
			transaction.commit();
			
		}catch (HibernateException e) { 
	        
			transaction.rollback(); 			           
			e.printStackTrace(); 
		       
		} finally { 		           
			session.close(); 		       
		} 
		
		return pacotes;		
	
	}
	
	@SuppressWarnings("unchecked")
	public Pacote buscarPacotesParaCD(int idPacote, int idProjeto, Session session){
		
		List<Pacote> pacotes = new ArrayList <Pacote> (); 
		Transaction transaction = null; 
				
		try{
			
		    transaction = session.beginTransaction(); 
		    pacotes = session.createCriteria(Pacote.class).add(Restrictions.eq("idProjeto", idProjeto)).add(Restrictions.eq("idPacote", idPacote)).list();	    
			transaction.commit(); 		

			
		} catch (HibernateException e) { 
	        
			transaction.rollback(); 		            
			e.printStackTrace(); 
		        
		}		
		
		if(pacotes.size() == 0)
			return null;
		else
			return pacotes.get(0);
								
	}
}
